$.extend($.fn.validatebox.defaults.rules, {
	validateTel : {
        validator : function(value, param) {
        	return (/^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/g.test(value));
        },
        message: '请输入正确的手机号码'
    }
});

AddEmployee = function() {
	return {
		
		saveEmployee : function() {
			$('#fm').form('submit', {
				url      : "/pc/employee/save.htm",
				onSubmit : function() {
					return $("#fm").form("validate");
				},
				success  : function(data) {
					if(0 == data) {
						Ict.info("添加成功!", function() {
							Ict.closeWin();
							$("#employeeGrid").datagrid("reload");
						});
					} else if(1 == data) {
						Ict.warn("手机号码已存在,请确认!");
					} else {
						Ict.error("添加失败,稍后重试!");
					}
				}
			});
		},
		
		getCompanyId : function() {
			var row = $('#companyDeptTree').tree('getSelected');
			var type_id = row.id.split("_");
			if("d" == type_id[0]) {
				$.ajax({
					url		:	"/pc/department/getCompanyId.htm",
					type	:	'POST',
					data	:	{
						'departmentId' : type_id[1]
					},
					dataType:	'JSON',
					async	:	false,
					success :	function(data) {
						AddEmployee.initDepartment(data, type_id[1]);
						AddEmployee.initHeadship(data);
					}
				});
			} else {
				AddEmployee.initDepartment(type_id[1], "0");
				AddEmployee.initHeadship(type_id[1]);
			}
		},
		
		initDepartment : function(companyId, departmentId) {
			$("#companyId").val(companyId);
			$("#departmentId").combotree({
				url           : '/pc/department/deptComTree.htm?companyId=' + companyId,
				editable      : false,
				required      : true,
				panelHeight   : 200,
				onLoadSuccess : function() {
					if("0" != departmentId) {
						$("#departmentId").combotree('setValue', departmentId);
					}
				}
			});
		},
		
		initHeadship : function(companyId) {
			$("#headshipId").combobox({
				url           : '/pc/employee/headships.htm?companyId=' + companyId,
				editable      : false,
				required      : true,
				panelHeight   : 200,
				valueField    : 'headshipId',
				textField     : 'headshipName'
			});
		},
		
		initRole : function() {
			$("#roleId").combobox({
				url           : '/pc/employee/roles.htm',
				editable      : false,
				required      : true,
				panelHeight   : 200,
				valueField    : 'roleId',
				textField     : 'roleName'
			});
		},
		
		init : function() {
			AddEmployee.initRole();
			AddEmployee.getCompanyId();
		}
	};
}();

$(function(){
	AddEmployee.init();
});